package com.fsj.spring.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.fsj.spring.dao.IDeptDao;
import com.fsj.spring.model.TDept;
import com.fsj.spring.service.IDeptService;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

//@Service("deptService")
@Service
public class DeptServiceImpl implements IDeptService {

//     @Autowired(required=true)
//       @Qualifier("deptDao")
//       @Resource(name="deptDao")
//    @Autowired
//    @Qualifier
    @Resource
    public IDeptDao deptDao;

    public IDeptDao getDeptDao() {
        return deptDao;
    }

    public void setDeptDao(IDeptDao deptDao) {
        this.deptDao = deptDao;
    }

    public void addDept(TDept dept) throws Exception {
        deptDao.save(dept);
    }

    public void deleteDept(int id) throws Exception {
        deptDao.delete(getDeptById(id));
    }

    public TDept getDeptById(int id) throws Exception {
        return deptDao.findById(id);
    }

    @SuppressWarnings("unchecked")
    public List<TDept> getDeptList() throws Exception {
        return deptDao.findAll();
    }

    public void updateDept(TDept dept) throws Exception {
        deptDao.merge(dept);
    }

    public Map<String, Object> getAllJson() throws Exception {
        Map<String, Object> result = new HashMap<String, Object>(2);
        List<TDept> list = getDeptList();
        result.put("total", list == null ? 0 : list.size());
        result.put("rows", list);
        return result;
    }
}
